function varargout=DistFun(SupportMat,AccMassMat,CutOffMat,TypeFlag)

% SupportMat  =   DistApp.X.MatGrid;
% AccMassMat  =   cumsum(DistMat_Xa);
% CutOffMat   =   [0.1,0.1,0.000];
N_Col       =   size(SupportMat,2);
N_Row       =   size(CutOffMat,1);
Num_Support =   size(SupportMat,1);

switch TypeFlag
    case 'CDF'
        CDFMat      =   zeros(N_Row,N_Col);
        for i=1:N_Col
            BracketInd      =   lookup(SupportMat(:,i),CutOffMat(:,i));
            BracketLeftInd  =   max(BracketInd,1);
            BracketLeft     =   min(CutOffMat(:,i),...
                                    SupportMat(BracketLeftInd,i));
            BracketRightInd =   min(BracketInd+1,Num_Support);
            BracketRight    =   max(CutOffMat(:,i),...
                                    SupportMat(BracketRightInd,i));
            WeightRight     =   ( CutOffMat(:,i)-BracketLeft )./ ...
                                ( BracketRight-BracketLeft );
            CDFMat(:,i)     =   (1-WeightRight).*AccMassMat(BracketLeftInd,i)+...
                                WeightRight.*AccMassMat(BracketRightInd,i);
        end
        varargout{1}        =   CDFMat;
    case 'Quantile'
        QtMat       =   zeros(N_Row,N_Col);
        for i=1:N_Col
            BracketInd      =   lookup(AccMassMat(:,i),CutOffMat(:,i));
            BracketLeftInd  =   max(BracketInd,1);
            BracketLeft     =   min(CutOffMat(:,i),...
                                    AccMassMat(BracketLeftInd,i));
            BracketRightInd =   min(BracketInd+1,Num_Support);
            BracketRight    =   max(CutOffMat(:,i),...
                                    AccMassMat(BracketRightInd,i));
            WeightRight     =   ( CutOffMat(:,i)-BracketLeft )./ ...
                                ( BracketRight-BracketLeft );
            QtMat(:,i)      =   (1-WeightRight).*SupportMat(BracketLeftInd,i)+...
                                WeightRight.*SupportMat(BracketRightInd,i);
        end
        varargout{1}        =   QtMat;
end